/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BLForEntities;

import Entities.Encrypter;
import Entities.Klant;
import Interfaces.RegistrerenInterface;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

/**
 *
 * @author Andy
 */
public class BLRegistreren implements RegistrerenInterface {

    private Connectie connectie;
    EntityManager em = null;
    EntityTransaction et = null;

    public BLRegistreren() {
        this.beginConnectie();
    }

    private void beginConnectie() {

        connectie = new Connectie();

        if (em == null) {
            em = connectie.getConnectie();
        }

    }

    @Override
    public boolean insertKlant(Klant klant) {



        try {
            Query q = em.createNamedQuery("Klant.getKlantByEmail");
            q.setParameter("email", klant.getEmail());
            Klant klantExistance = (Klant) q.getSingleResult();

            return false;
        } catch (Exception exc) {
            this.beginConnectie();
            //Encrypt password
            klant.setWachtwoord(Encrypter.Shaa1(klant.getWachtwoord()));
            et = em.getTransaction();
            et.begin();
            em.persist(klant);
            et.commit();

            return true;
        }
    }
}
